package com.slipper.system.codeless.controller;

import com.slipper.codeless.service.CodelessService;
import com.slipper.common.bo.UserDetail;
import com.slipper.constants.Constants;
import com.slipper.exception.CommonException;
import com.slipper.util.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RequestMapping("/system/codeless")
@RestController
public class CodelessController {
    @Resource
    public CodelessService codelessService;

    /**
     * 查询数据模型数据
     * @param dataModelId 数据模型ID
     * @param data 查询条件
     */
    @PostMapping("/{dataModelId}/page")
    public String queryPage(
            @PathVariable("dataModelId") String dataModelId,
            @RequestBody Map<String, Object> data) {
        if (Tool.isBlank(dataModelId)) return ResultUtil.failureResult("参数错误！");
        return ResultUtil.successResult(codelessService.queryPage(dataModelId, data));
    }

    /**
     * 保存页面组件树节点数据
     */
    @PostMapping("/{dataModelId}/save")
    public String save(@PathVariable("dataModelId") String dataModelId, @RequestBody Map<String, Object> data) {
        UserDetail user = Constants.getCurrentUser();
        return ResultUtil.saveResult(codelessService.saveByDataModelId(dataModelId, user, data, null));
    }

    /**
     * 删除页面组件数据
     */
    @PostMapping("/{dataModelId}/delete")
    public String delete(@PathVariable("dataModelId") String dataModelId, @RequestBody Map<String, Object> data) {
        if (Tool.isBlank(dataModelId)) throw new CommonException("参数错误！");
        Constants.getCurrentUser();
        return ResultUtil.deleteResult(codelessService.deleteByDataModelId(dataModelId, data));
    }
}
